package cn.etynit.web.controller;

import cn.etynit.service.IMovieTicketService;
import cn.etynit.domain.MovieTicket;
import cn.etynit.query.MovieTicketQuery;
import cn.etynit.result.JsonResult;
import cn.etynit.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/movieTicket")
public class MovieTicketController {

    @Autowired
    public IMovieTicketService movieTicketService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody MovieTicket movieTicket){
        if(movieTicket.getId()!=null){
            movieTicketService.updateById(movieTicket);
        }else{
            movieTicketService.insert(movieTicket);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        movieTicketService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(movieTicketService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(movieTicketService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody MovieTicketQuery query){
        Page<MovieTicket> page = new Page<MovieTicket>(query.getPage(),query.getRows());
        page = movieTicketService.selectPage(page);
        return JsonResult.success(new PageList<MovieTicket>(page.getTotal(),page.getRecords()));
    }

    /**
     * 买电影票
     * @param movieId
     * @param count
     * @return
     */
    @RequestMapping(value = "/buy/{movieId}/{count}",method = RequestMethod.GET)
    public JsonResult byMovie(@PathVariable Long movieId,@PathVariable Long count){
        return movieTicketService.byMovie(movieId,count);
    }

    @RequestMapping(value = "/giving/{movieId}/{count}/{userId}",method = RequestMethod.GET)
    public JsonResult giving(@PathVariable Long movieId,@PathVariable Long count,@PathVariable Long userId){
        return movieTicketService.giving(movieId,count,userId);
    }
}
